<template>
  <div class="software-detail">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <div class="container">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/software' }">软件下载</el-breadcrumb-item>
          <el-breadcrumb-item>{{ software?.name || '软件详情' }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>

    <!-- 页面内容 -->
    <section class="software-content">
      <div class="container">
        <!-- 加载状态 -->
        <div v-if="loading" class="loading-state">
          <el-skeleton :rows="6" animated />
        </div>
        
        <!-- 404状态 -->
        <el-empty v-else-if="!software" description="未找到该软件" class="not-found">
          <el-button type="primary" @click="$router.push('/software')">返回软件中心</el-button>
        </el-empty>
        
        <!-- 软件详情内容 -->
        <div v-else class="software-info">
          <!-- 软件头部信息 -->
          <div class="software-header">
            <div class="software-cover">
              <el-image
                :src="software.cover_image || placeholderImage"
                :alt="software.name"
                fit="cover"
                class="cover-image"
              >
                <template #error>
                  <div class="image-error">
                    <el-icon><Picture /></el-icon>
                  </div>
                </template>
              </el-image>
              <el-tag type="success" class="download-count">
                <el-icon><Download /></el-icon>
                {{ formatNumber(software.download_count) }}次下载
              </el-tag>
            </div>
            
            <div class="software-meta">
              <h1 class="software-title">{{ software.name }}</h1>
              
              <div class="meta-grid">
                <div class="meta-item">
                  <span class="meta-label">版本：</span>
                  <el-tag size="large" type="info">{{ software.version }}</el-tag>
                </div>
                
                <div class="meta-item">
                  <span class="meta-label">分类：</span>
                  <el-tag size="large" :type="getCategoryType(software.category)">
                    {{ getCategoryLabel(software.category) }}
                  </el-tag>
                </div>
                
                <div class="meta-item">
                  <span class="meta-label">更新：</span>
                  <span>{{ formatDate(software.updated_at) }}</span>
                </div>
                
                <div class="meta-item">
                  <span class="meta-label">大小：</span>
                  <span>{{ software.file_size || '--' }}</span>
                </div>
              </div>
              
              <div class="action-buttons">
                <el-button 
                  type="primary" 
                  size="large" 
                  @click="downloadSoftware"
                  class="download-btn"
                >
                  <el-icon><Download /></el-icon>
                  立即下载
                </el-button>
                
                <el-button 
                  type="success" 
                  size="large" 
                  @click="showScreenshotDialog"
                  v-if="software.screenshots?.length"
                >
                  <el-icon><Picture /></el-icon>
                  查看截图
                </el-button>
              </div>
              
              <div class="quick-info">
                <el-alert 
                  v-if="software.is_official" 
                  title="官方正版" 
                  type="success" 
                  :closable="false"
                  show-icon
                />
                <el-alert 
                  v-if="software.is_free" 
                  title="免费软件" 
                  type="info" 
                  :closable="false"
                  show-icon
                />
              </div>
            </div>
          </div>
          
          <!-- 软件详情标签页 -->
          <el-tabs v-model="activeTab" class="software-tabs">
            <el-tab-pane label="软件介绍" name="description">
              <div class="tab-content">
                <h3>软件描述</h3>
                <div class="description-content" v-html="software.description"></div>
                
                <h3 v-if="software.features?.length">软件特性</h3>
                <ul class="feature-list" v-if="software.features?.length">
                  <li v-for="(feature, index) in software.features" :key="index">
                    <el-icon><Check /></el-icon>
                    {{ feature }}
                  </li>
                </ul>
              </div>
            </el-tab-pane>
            
            <el-tab-pane label="系统要求" name="requirements">
              <div class="tab-content">
                <el-descriptions :column="1" border>
                  <el-descriptions-item label="操作系统">
                    {{ software.os_requirement || 'Windows 7及以上' }}
                  </el-descriptions-item>
                  <el-descriptions-item label="处理器">
                    {{ software.processor || 'Intel Core i3及以上' }}
                  </el-descriptions-item>
                  <el-descriptions-item label="内存">
                    {{ software.memory || '4GB RAM' }}
                  </el-descriptions-item>
                  <el-descriptions-item label="存储空间">
                    {{ software.storage || '1GB可用空间' }}
                  </el-descriptions-item>
                  <el-descriptions-item label="显卡" v-if="software.graphics">
                    {{ software.graphics }}
                  </el-descriptions-item>
                </el-descriptions>
              </div>
            </el-tab-pane>
            
            <el-tab-pane label="更新日志" name="changelog" v-if="software.changelog?.length">
              <div class="tab-content">
                <el-timeline>
                  <el-timeline-item
                    v-for="(log, index) in software.changelog"
                    :key="index"
                    :timestamp="formatDate(log.date)"
                    placement="top"
                  >
                    <el-card>
                      <h4>版本 {{ log.version }}</h4>
                      <ul>
                        <li v-for="(item, i) in log.items" :key="i">{{ item }}</li>
                      </ul>
                    </el-card>
                  </el-timeline-item>
                </el-timeline>
              </div>
            </el-tab-pane>
          </el-tabs>
          
        </div>
      </div>
    </section>

    <!-- 截图对话框 -->
    <el-dialog v-model="screenshotDialogVisible" title="软件截图" width="80%">
      <el-carousel :interval="5000" height="500px">
        <el-carousel-item v-for="(img, index) in software.screenshots" :key="index">
          <el-image 
            :src="img" 
            fit="contain" 
            style="width: 100%; height: 100%"
          />
        </el-carousel-item>
      </el-carousel>
    </el-dialog>

  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getSoftwareById } from '@/api/software'
import { 
  Download, Picture, Check
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import placeholderImage from '@/assets/default-software.png'

const route = useRoute()

// 数据状态
const software = ref(null)
const loading = ref(true)
const activeTab = ref('description')
const screenshotDialogVisible = ref(false)

// 模拟获取软件详情
const fetchSoftwareDetail = async () => {
  try {
    loading.value = true
    const softwareId = route.params.id
    software.value = await getSoftwareById(softwareId)
  } catch (error) {
    ElMessage.error('获取软件详情失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

// 下载软件
const downloadSoftware = () => {
  ElMessage.success('开始下载: ' + software.value.name)
  // 实际项目中这里应该是下载逻辑
  window.open(`/download/${software.value.id}`, '_blank')
}

// 显示截图对话框
const showScreenshotDialog = () => {
  screenshotDialogVisible.value = true
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 格式化数字
const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num.toString()
}

// 获取分类标签类型
const getCategoryType = (category) => {
  const typeMap = {
    'office': 'primary',
    'design': 'success',
    'development': 'warning',
    'security': 'danger',
    'entertainment': 'info',
    'other': 'default'
  }
  return typeMap[category] || 'default'
}

// 获取分类标签文本
const getCategoryLabel = (category) => {
  const labelMap = {
    'office': '办公软件',
    'design': '设计软件',
    'development': '开发工具',
    'security': '安全软件',
    'entertainment': '娱乐软件',
    'other': '其他'
  }
  return labelMap[category] || category
}

// 初始化加载数据
onMounted(() => {
  fetchSoftwareDetail()
})
</script>

<style scoped lang="scss">
.software-detail {
  background-color: #f5f7fa;
  min-height: 100vh;
}

.breadcrumb {
  padding: 16px 0;
  background-color: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  
  .container {
    padding: 0 20px;
  }
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.software-content {
  padding: 30px 0;
}

.loading-state {
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.not-found {
  background: white;
  padding: 60px 0;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.software-info {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.software-header {
  display: flex;
  flex-wrap: wrap;
  gap: 30px;
  padding: 30px;
  background-color: #f9f9f9;
  
  @media (max-width: 768px) {
    flex-direction: column;
    padding: 20px;
  }
}

.software-cover {
  position: relative;
  flex: 0 0 300px;
  
  @media (max-width: 768px) {
    flex: 1;
  }
}

.cover-image {
  width: 100%;
  height: 250px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  
  :deep(.el-image__inner) {
    transition: transform 0.3s ease;
  }
  
  &:hover :deep(.el-image__inner) {
    transform: scale(1.05);
  }
}

.image-error {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f1f5f9;
  color: #cbd5e1;
  
  .el-icon {
    font-size: 50px;
  }
}

.download-count {
  position: absolute;
  bottom: 15px;
  left: 15px;
  font-size: 14px;
  font-weight: bold;
  
  .el-icon {
    margin-right: 5px;
  }
}

.software-meta {
  flex: 1;
  min-width: 300px;
}

.software-title {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 28px;
  font-weight: 700;
}

.meta-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  margin-bottom: 25px;
  
  @media (max-width: 480px) {
    grid-template-columns: 1fr;
  }
}

.meta-item {
  display: flex;
  align-items: center;
  font-size: 15px;
  
  .meta-label {
    color: #666;
    margin-right: 8px;
    min-width: 60px;
  }
  
  .el-tag {
    font-size: 14px;
  }
}

.action-buttons {
  display: flex;
  gap: 15px;
  margin: 25px 0;
  
  .el-button {
    flex: 1;
    
    .el-icon {
      margin-right: 5px;
    }
  }
  
  @media (max-width: 480px) {
    flex-direction: column;
  }
}

.quick-info {
  margin-top: 20px;
  
  .el-alert {
    margin-bottom: 10px;
  }
}

.software-tabs {
  padding: 0 30px;
  
  @media (max-width: 768px) {
    padding: 0 20px;
  }
  
  :deep(.el-tabs__header) {
    margin: 0;
  }
  
  :deep(.el-tabs__nav-wrap)::after {
    height: 1px;
  }
}

.tab-content {
  padding: 20px 0 30px;
  
  h3 {
    margin: 0 0 15px 0;
    color: #333;
    font-size: 20px;
    font-weight: 600;
  }
}

.description-content {
  line-height: 1.8;
  color: #444;
  
  p {
    margin: 0 0 15px 0;
  }
  
  ul {
    padding-left: 20px;
    margin: 0 0 15px 0;
  }
}

.feature-list {
  list-style: none;
  padding: 0;
  margin: 0;
  
  li {
    padding: 8px 0;
    border-bottom: 1px dashed #eee;
    display: flex;
    align-items: center;
    
    .el-icon {
      color: #67c23a;
      margin-right: 8px;
      font-size: 14px;
    }
    
    &:last-child {
      border-bottom: none;
    }
  }
}

.related-software {
  padding: 30px;
  border-top: 1px solid #eee;
  
  @media (max-width: 768px) {
    padding: 20px;
  }
}

.section-title {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 22px;
  font-weight: 600;
}

.related-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.app-footer {
  background-color: #2d3748;
  color: white;
  padding: 50px 0 20px;
  
  .container {
    padding: 0 20px;
  }
}
</style>